package jmini.examples.spring;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import jmini.utils.Str;


public class Props {

	private static Properties prop = new Properties();
	
	/**
	 * 重新装载配置文件 默认编码UTF-8
	 * @param file
	 * @throws IOException 
	 */
	public static void load(File file) throws IOException{
		load(file, "UTF-8");
	}
	
	/**
	 * 重新装载配置文件
	 * @param file 文件
	 * @param encoding 编码
	 * @throws IOException 
	 */
	public static void load(final File file, final String encoding) throws IOException{
		String newEncoding = "UTF-8";
		if(!Str.isEmpty(encoding)){
			newEncoding = encoding;
		}
		if(file != null){
			InputStreamReader is = null;
			try {
				is = new InputStreamReader(new FileInputStream(file), newEncoding);
				prop.load(is);
			}finally{
				if(is != null){
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}
	
	/**
	 * 读取单个系统属性
	 * @param key
	 * @return
	 */
	public static String p(String key){
		return prop.getProperty(key,null);
	}
	
	/**
	 * 设置单个系统属性
	 * @param key
	 * @param value
	 */
	public static void p(String key,String value){
		prop.setProperty(key, value);
	}
	
}
